<template>
  <div
    class="box-border w-full h-full bg-[#6784b1] px-[60px] py-[30px] flex items-center justify-between"
  >
    <div>
      <img src="../assets/images/logo.png" class="w-[193px] h-[43px]" />
    </div>
    <div class="flex items-center gap-[128px]">
      <div class="relative flex items-center gap-[96px] box-border px-12">
        <div
          v-for="item in modules"
          :key="item.id"
          class="text-[#fff]/70 text-[22px] cursor-pointer relative hover:text-[#fff]"
          :class="{ '!text-[#fff]': moduleStore.activeModuleIndex === item.id }"
          @click="handleModuleClick(item.id)"
        >
          {{ item.name }}
        </div>
        <div
          class="absolute bottom-[-25px] h-[8px] w-[178px] bg-[#49A9FF] transition-all duration-300"
          :style="{ left: `${moduleStore.activeModuleIndex * 186}px` }"
        ></div>
      </div>
      <div class="flex items-center gap-[20px]">
        <img src="../assets/images/userImg.png" class="w-[40px] h-[40px]" />

        <div class="text-[#fff] text-[22px]">用户名</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter, useRoute } from 'vue-router'
import { onMounted, watch } from 'vue'
import { useModuleStore } from '@/stores/module'

const router = useRouter()
const route = useRoute()
const moduleStore = useModuleStore()

const modules = [
  {
    id: 0,
    name: '装配检测',
  },
  {
    id: 1,
    name: '模型管理',
  },
  {
    id: 2,
    name: '相机管理',
  },
]
watch(
  () => route.path,
  (val) => {
    // 如果进入/task_starting页面，强制切换到装配检测模块
    if (val === '/task_starting') {
      moduleStore.activeModuleIndex = 0
    }
  },
)
const handleModuleClick = (id) => {
  // 如果当前路径是/task_starting，且尝试切换到模型或相机管理，则不执行切换
  if (route.path === '/task_starting' && (id === 1 || id === 2)) {
    return
  }
  moduleStore.activeModuleIndex = id
}

watch(
  () => moduleStore.activeModuleIndex,
  (newVal) => {
    if (newVal === 0) {
      router.push('/')
    } else if (newVal === 1) {
      router.push('/model_home')
    } else if (newVal === 2) {
      router.push('/camera')
    }
  },
  {
    immediate: true,
  },
)
</script>

<style lang="scss" scoped></style>
